// masterWorker/pages/dati/dati.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 1,
    isYes: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var id = options.id
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: 'https://api.wechat.ahutiku.com/book/get_title?section_id=' + id,
      success(res) {
        that.setData({
          title: res.data.data
        })
      }
    })
    wx.request({
      url: 'https://api.wechat.ahutiku.com/book/list?section_id=' + id,
      success(res) {
        wx.hideLoading();
        console.log(res.data)
        that.setData({
          question: res.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  radioChange(e) {
    console.log(e)
    var that = this;
    that.setData({
      isClick: true,
      isYes: e.detail.value === that.data.question.page_data[that.data.currentIndex].answer,
      userAnswer: e.detail.value
    })
  },
  previous(e) {
    var that = this;
    that.setData({
      isClick: false,
      isChecked: false,
      currentIndex: that.data.currentIndex - 1 <= 0 ? 1 : that.data.currentIndex - 1
    })
  },
  next(e) {
    var that = this;
    that.setData({
      isClick: false,
      isChecked: false,
      currentIndex: that.data.currentIndex + 1 > that.data.question.page_total_count ? that.data.question.page_total_count : that.data.currentIndex + 1
    })
  },
  touchStart(e) {
    this.setData({
      "touch.x": e.changedTouches[0].clientX,
      "touch.y": e.changedTouches[0].clientY
    });
  },
  touchEnd(e) {
    let x = e.changedTouches[0].clientX;
    let y = e.changedTouches[0].clientY;
    this.getTouchData(x, y, this.data.touch.x, this.data.touch.y);
  },
  getTouchData(endX, endY, startX, startY) {
    var that = this;
    if (endX - startX > 30 && Math.abs(endY - startY) < 30) { //右滑
      that.previous();
    } else if (endX - startX < -30 && Math.abs(endY - startY) < 30) { //左滑
      that.next();
    }
  }
})